$(".header").load("header_top.html")
$(".bottom").load("bottom.html");
//从数据库获取数据
$.ajax({
	type: "get",
	url: "shoppings.php",
	async: true,
	dataType: "json",
	data: {
		flag: "select"
	},
	success: function(data) {
		console.log(data);
		for (var i = 0; i < data.length; i++) {
			var shop = $('<li><div class="Lileft"><img src="' + data[i].trade_img + '"alt=""/><span>' + data[i].trade_name + '</span></div><div class="LiRight"><div><p>尺寸:<span class="chicun">' + data[i].trade_size + '</span></p><p>颜色:<span class="color">' + data[i].trade_color + '</span></p></div><span><span>￥</span><span class="danjia">' + data[i].trade_price + '</span></span><span class="shuliang">' + data[i].trade_num + '</span><span><span>￥</span><span class="zong1">' + data[i].trade_yige + '</span></span></div></li>');
			$(".GoodAll").prepend(shop);
		}
		$(".shangpinjine").text(data[0].trade_zongjia);
		var c = parseInt($(".shangpinjine").text() - $(".yunfei").text());
		$(".dianpuzongji").text(c);
		$(".qian11").text(c);
		$(".yingfujine").text($(".qian11").text()-parseInt($(".jifen").val()/100)-parseFloat($(".jifen").val()%100)/100);
		localStorage.yingfujine=$(".yingfujine").text();
	$(".jifen").change(function(){
		console.log(parseFloat($(this).val()));
		console.log(parseInt($(this).val()/100));
		console.log(parseFloat($(this).val()%100));
		$(".zhengshu").text(parseInt($(this).val()/100)+".");
		$(".xiaoshu").text(parseFloat($(this).val()%100));
		$(".yingfujine").text($(".qian11").text()-parseInt($(this).val()/100)-parseFloat($(this).val()%100)/100);
		localStorage.yingfujine=$(".yingfujine").text();
	})
	}
	
})

function Trim(str) {
	return str.replace(/(^\s*)|(\s*$)/g, "");
}
var orig_detail;
//<--------------------------请求收货地址,并显示在当前的页面上------------------>
$.ajax({
	type: "get",
	url: "express_info.php",
	async: true,
	dataType: "json",
	data: {
		flag: "select",
	},
	success: function(data) {
		var previous = 0;
		for (var i = 0; i < data.length; i++) {
			var li = $('<li><img src="./img/sure_order/noselect.png" alt=""><p class="expre_name">' + data[i].expre_name + '</p> <p class="expre_district"> ' + data[i].expre_district + '</p> <p class="expre_detail">' + data[i].expre_detail + '</p>固定电话：<p class="fixed_tel">' + data[i].fixed_tel + ' </p>手机号：<p class="expre_tel">' + data[i].expre_tel + '</p><span class="default">默认地址</span><span class="set_default">设为默认地址</span><a href="#" class="update_addr">修改</a><a href="#" class="delete">删除</a></li>')
			$(".receliverAll").prepend(li);
			// 修改地址按钮的点击事件
			//设置是否为默认地址
			if (parseInt(data[i].set_default)) {
				li.find(".default").css("opacity", "1")
				li.find(".set_default").css("opacity", "0");
				li.find("img").attr("src", "img/sure_order/select.png");
				previous = data.length - 1 - i;
			}
			//<------------------------设为默认地址的点击事件------------------------------->
		}
		$(".set_default").click(function() {
				//清除上次
				$(".default").eq(previous).css("opacity", "0");
				$(".set_default").eq(previous).css("opacity", "1");
				$(".receliverAll li").eq(previous).find("img").attr("src", "img/sure_order/noselect.png");
				//设置本次
				$(this).css("opacity", "0");
				$(this).prev().css("opacity", "1");
				$(this).parents("li").find("img").attr("src", "img/sure_order/select.png")
				previous = $(this).parents("li").index();
				// 将修改后的默认地址存储到数据库中
				$.ajax({
					type: "get",
					url: "express_info.php",
					async: true,
					data: {
						expre_detail: $(this).parents("li").find(".expre_detail").text(),
						flag: "set_default"
					},
					success: function(data) {

					}
				});
			})
			//<--------------------------删除地址--------------------------->
		$(".delete").click(function() {
				$.ajax({
					type: "get",
					url: "express_info.php",
					async: true,
					data: {
						flag: "delete",
						expre_detail: $(this).parent().find(".expre_detail").text()
					},
					success: function(data) {
						if (data == "1") {
							alert("删除成功");
							window.location.reload();
						}
					}
				});
			})
			//<----------------------修改按钮的点击事件-------------------------->
		$(".update_addr").click(function() {
			orig_detail = $(this).parent().find(".expre_detail").text();
			// 显示修改地址界面
			$("#address_manager").css("display", "block");
			$(".save").text("修改");
			$("#delivery_li1inp").val($(this).parent().find(".expre_name").text());
			//以后修改
			var expre_district = $(this).parent().find(".expre_district").text();
			var province = expre_district.split(" ")[1];
			var city = expre_district.split(" ")[2];
			var district = expre_district.split(" ")[3];
			//设置默认省
			for (var i = 0; i < $("#province > option").length; i++) {
				if ($("#province > option").eq(i).text() == Trim(province)) {
					$("#province > option").eq(i).prop("selected", true);
				}
			}
			//设置默认市
			$("#city > option").text(city);
			//设置默认区
			$("#district > option").text(district)
				// 详细地址
			$("#delivery_li3inp").val($(this).parent().find(".expre_detail").text());
			// 手机号
			$("#delivery_li4inp1").val($(this).parent().find(".expre_tel").text());
			var district_num = $(this).parent().find(".fixed_tel").text().split("-", 2)[0];
			var district_tel = $(this).parent().find(".fixed_tel").text().split("-", 2)[1];
			$("#delivery_li4inp2").val(district_num);
			$("#delivery_li4inp3").val(district_tel);
		})
	}
});
//<------------------------三级联动------------------------->
//创建变量存储选择的省--市--区
//var province_val;
//var city_val;
var district_val;
var province_num;
var city_num;
for (var item_pro in ChineseDistricts[86]) {
	var option = $('<option value=' + item_pro + '>' + ChineseDistricts[86][item_pro] + '</option>');
	$("#province").append(option);
}
//省的点击事件
$("#province").change(function() {
	city_select();
	district_select();
	//	打印的是改变后的省名
})

function city_select() {
	province_num = $("#province").val();
	// 清除所有的子级元素
	$("#city").empty();
	for (var item_city in ChineseDistricts[province_num]) {
		var option = $('<option value=' + item_city + '>' + ChineseDistricts[province_num][item_city] + '</option>');
		$("#city").append(option);
	}
}
//市的点击事件
$("#city").change(function() {
	district_select();
	//	打印的是改变后的市名
})

function district_select() {
	city_num = $("#city").val();
	// 清除所有的子级元素
	$("#district").empty();
	for (var item_dis in ChineseDistricts[city_num]) {
		var option = $('<option vlaue=' + item_dis + '>' + ChineseDistricts[city_num][item_dis] + '</option>');
		$("#district").append(option);
	}
}
$("#district").change(function() {
		//	打印的是改变后的县名
		console.log($("#district").val())
	})
	//是否设为常用地址
var bool = false;
$(".li_img").click(function() {
		if (!bool) {
			$(this).attr("src", "img/sure_order/select.png");
			bool = true;
		} else {
			$(this).attr("src", "img/sure_order/noselect.png");
			bool = false;
		}
	})
	//添加地址
$(".newAddress").click(function() {
		$("#address_manager").css("display", "block");
	})
	//返回
$(".return").click(function() {
		$("#address_manager").css("display", "none");
	})
	//保存
$(".save").click(function() {
	// 判断该地址是否设为默认地址
	var set_default;
	if ($(".li_img").attr("src") == "img/sure_order/select.png") {
		set_default = 1;
	} else {
		set_default = 0;
	}
	if ($(this).text() == "保存") {
		// 添加地址
		$.ajax({
			type: "get",
			url: "express_info.php",
			async: true,
			data: {
				expre_name: $("#delivery_li1inp").val(),
				expre_district: $("#province > option:selected").text() + " " + $("#city > option:selected").text() + " " + $("#district").val(),
				expre_detail: $("#delivery_li3inp").val() + " ",
				expre_tel: $("#delivery_li4inp1").val(),
				fixed_tel: $("#delivery_li4inp2").val() + "-" + $("#delivery_li4inp3").val(),
				set_default: set_default,
				flag: "insert"
			},
			success: function(data) {
				if (data == "1") {
					alert("地址添加成功");
					window.location.reload();
				}
			}
		});
	} else {
		//<--------------------------------------确认修改---------------------------------------->
		$.ajax({
			type: "get",
			url: "express_info.php",
			async: true,
			data: {
				expre_name: $("#delivery_li1inp").val(),
				expre_district: $("#province > option:selected").text() + " " + $("#city > option:selected").text() + " " + $("#district > option:selected").text(),
				expre_detail: $("#delivery_li3inp").val() + " ",
				expre_tel: $("#delivery_li4inp1").val(),
				fixed_tel: $("#delivery_li4inp2").val() + "-" + $("#delivery_li4inp3").val(),
				orig_detail: orig_detail,
				set_default: set_default,
				flag: "update"
			},
			success: function(data) {
				if (data == "1") {
					alert("地址修改成功");
					$(".save").text() == "保存";
					window.location.reload();
				}
			}
		});
	}
	$("#address_manager").css("display", "none");
})
var bool1 = false;
$(".moreaddress").click(function() {
	if (!bool1) {
		$(".moreaddress span").html("收起地址");
		$(".moreaddress img").attr("src", "img/sure_order/up.png");
		//展开
		$(".receliverAll li").css("display", "block");
		bool1 = true;
	} else {
		$(".moreaddress span").html("展开其他地址");
		$(".moreaddress img").attr("src", "img/sure_order/down.png");
		//隐藏
		$(".receliverAll li").css("display", "none");
		$(".receliverAll li").eq(1).css("display", "block");
		$(".receliverAll li").eq(2).css("display", "block");
		$(".receliverAll li").eq(3).css("display", "block");
		bool1 = false;
	}
});

//更新选中开票信息选中按钮
var bool=true;
$(".receipttop img").click(function(){
	if(bool){
		$(".receipttop img").attr("src","img/sure_order/noselect.png");
		$(this).attr("src","img/sure_order/select.png");
		bool=false;
	}else{
		$(this).attr("src","img/sure_order/noselect.png");
		bool=true;
	}
	
})
//跳转页面
var a=b=c=false;
$(".SubmitOrder button").click(function() {
	$(".receipttop").find("img").each(function(){
		if($(this).attr("src")=="img/sure_order/select.png"){
			a=true;
		}
	})
	if(a==false){
		alert("请选择开票信息");	
	}
	if($(".ReceiptTitle input").val()){
			var b=true;
	}else{
			alert("请填写发票抬头");
		}
	if($(".AddRemark textarea").val()){
			var c=true;
			console.log("c:"+c);
	}else{
			alert("请添加订单备注");
		}
	
	if(a&&b&&c){
		window.location = "SuccessfulPay.html";
	}
})
